<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_user_notice".
 *
 * @property string $id
 * @property string $content
 * @property integer $level
 * @property integer $createtime
 * @property integer $forrole
 * @property integer $foruser
 * @property integer $status
 */
class BusinessUserNotice extends \yii\db\ActiveRecord
{

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '后台提醒',      //用于修改了什么的显示
        'mainfield' => 'title',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];


    const LEVEL_COMMON = 1;
    const LEVEL_MAIN = 2;
    const LEVEL_EMERGENCY = 3;
    public static $levels =[
        self::LEVEL_COMMON => '普通',
        self::LEVEL_MAIN => '重要',
        self::LEVEL_EMERGENCY => '紧急',
    ];


    const STATUS_NOMAL = 0;
    const STATUS_PUBLISH = 1;
    public static $statues = [
        self::STATUS_PUBLISH => '发布',
        self::STATUS_NOMAL => '不发布'
    ];



    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_user_notice';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title','level','content', 'status'], 'required'],
            [['title'], 'string', 'max' => 255],
            [['forrole','foruser'], 'integer'],
            [['content'], 'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'content' => '通知内容',
            'level' => '重要程度',
            'createtime' => '发布时间',
            'forrole' => '针对的角色',
            'foruser' => '针对的用户',
            'status' => '状态',
        ];
    }


    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @param $level
     * @return string
     * 封装后台首页的消息类颜色渲染
     */
    public static function renderLevel($level)
    {
        if($level == BusinessUserNotice::LEVEL_COMMON){
            return 'success';
        }else if($level == BusinessUserNotice::LEVEL_MAIN){
            return 'warning';
        }else if($level == BusinessUserNotice::LEVEL_EMERGENCY){
            return 'important';
        }

    }
}
